﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.Collections.ObjectModel;
using System.Text;
using System.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace TractRecord.Authenticated
{
    public partial class Project : TractRecordBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] == null || string.IsNullOrEmpty(Request["id"]))
            {
                // Catch where browse to page without project key
                Response.Redirect("Projects.aspx");
            }

            RegisterJavaScriptVariable("id", Request["id"]);



        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList GetProject(string ProjectId)
        {
            ArrayList values = new ArrayList();

            try
            {
                string userid = Membership.GetUser().ProviderUserKey.ToString();

                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectSummaryForUser_Get");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", userid));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProjectId", ProjectId));

                IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

                while (r.Read())
                {
                    values.Add(
                        new
                        {
                            ProjectId = r["ProjectId"].ToString(),
                            ProjectLink = r["Name"].ToString(),
                            Name = r["Name"].ToString(),
                            Description = r["Description"].ToString(),
                            ActiveInd = r["ActiveInd"].ToString(),
                            EmailId = r["EmailId"].ToString(),
                            EntryDt = WebCommon.FormatDate(r["EntryDt"].ToString(), "G"),
                            EmailLink = r["EntryDt"].ToString(),
                            EmailAddress = r["EmailAddress"].ToString(),
                            EmailCount = r["EmailCount"].ToString()
                        });
                }
            }
            catch (Exception ex)
            {
                Code.Common.Log(ex.ToString(), ex.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Critical, "");
            }
            return values;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList GetEmails(string ProjectId, bool ArchivedInd)
        {
            ArrayList values = new ArrayList();

            try
            {
                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectEmails_Get");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProjectId", ProjectId));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Archived", ArchivedInd));
                

                IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

                while (r.Read())
                {
                    values.Add(
                        new
                        {
                            FileId = r["FileId"].ToString(),
                            FileName = r["FileName"].ToString(),
                            MimeType = r["MimeType"].ToString(),
                            Subject = r["Subject"].ToString(),
                            EntryDt = r["EntryDt"].ToString(),
                            EmailAddress = r["EmailAddress"].ToString()
                        });
                }
            }
            catch (Exception ex)
            {
                Code.Common.Log(ex.ToString(), ex.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Critical, "");
            }
            
            return values;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void UpdateEmail(string FileId, bool? Archived, bool? Deleted)
        {
            try
            {
                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectFiles_Upd");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileId", FileId));

                if (Archived != null)
                {
                    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ArchivedInd", Archived));
                }

                if (Deleted != null)
                {
                    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@DeletedInd", Deleted));
                }

                Code.DataAccess.Data.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                Code.Common.Log(ex.ToString(), ex.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Critical, "");
            }
        }

    }
}